import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import refresh from '@dfeidao/atom-web/url/refresh';
import { productid } from '../../atom/config';
import get from '@dfeidao/atom-web/local/get';
export default async function a003(fd: IFeidaoAiBrowserComponent) {
	// todo
	const equipment_kind_no = fd.data.node.querySelector<HTMLInputElement>('[data-id="equipment_kind_no"]').value;
	console.log(equipment_kind_no);

	const equipment_kind_name = fd.data.node.querySelector<HTMLInputElement>('[data-id="equipment_kind_name"]').value;
	console.log('equipment_kind_name', equipment_kind_name);

	if (!equipment_kind_name) {
		alert("请填写设备种类名称");
		return;
	}
	const no = get(fd, 'no');
	//console.log('sss', no);

	const res1 = await nodejs<Array<{ equipment_kind_name: string }>>('device-type/zj-000008/s003', {
		filter: {
			equip_type_no: no
		}
	});

	const arr: String[] = [];
	res1.forEach((v) => {
		arr.push(v.equipment_kind_name);
	});
	console.log('zj8a003arr', arr);
	for (let i = 0; i < arr.length; i++) {
		if (equipment_kind_name === arr[i]) {
			alert("不能添加重复设备种类名称！");
			return;
		}
	}


	const res = await nodejs<{ code: 0 | 1; result: string }>('device-type/zj-000008/s002', {
		filter: {
			productid,
			equipment_kind_no: equipment_kind_no,
			equipment_kind_name: equipment_kind_name,
			equip_type_no: no
		}
	});
	console.log(res.code);
	if (res.code === 1) {
		alert('添加成功');
		refresh();
	} else {
		alert('添加失败');
		refresh();
	}
}
